package com.zy.config.controller;


import com.zy.common.core.R;
import com.zy.config.dto.AppConfigDetailsDto;
import com.zy.config.service.RemoteConfigProvider;
import com.zy.config.utils.PropertiesUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("config/")
public class ConfigController {

    @Autowired
    private RemoteConfigProvider provider;


    @PostMapping("addConfig")
    public R addConfig(@RequestBody AppConfigDetailsDto dto) {
        System.out.println(dto);
        return R.ok(dto);
    }

    @GetMapping("/property/{appId}")
    public R property(@PathVariable("appId") String appId) {
        return R.ok(PropertiesUtil.prop2YmlString(provider.load(appId)));
    }

    @GetMapping("propertyToString/{appId}")
    public R propertyToString(@PathVariable("appId") String appId) {
        return R.ok(provider.getStringProperties(appId));
    }

    @GetMapping("propertyToList/{appId}")
    public R<List<AppConfigDetailsDto>> propertyToList(@PathVariable("appId") String appId) {
        return R.ok(provider.getListProperties(appId));
    }
}
